package com.lindroy.androidplayer.ui.webview

import android.os.Bundle
import android.widget.FrameLayout
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.just.agentweb.AgentWeb
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseFragment
import com.lindroy.androidplayer.databinding.FragmentWebViewBinding
import com.lindroy.androidplayer.logic.model.bean.WebViewBean
import com.lindroy.androidplayer.utils.toHtml

/**
 * @author Lin
 * @date 2022/2/7
 * @function WebView 页面
 * @description 参数传递使用 Safe Args
 */
class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {

    private val webArgs: WebViewFragmentArgs by navArgs()

    private val webInfo by lazy { webArgs.webInfo }

    private var agentWeb: AgentWeb? = null

    private val preWeb: AgentWeb.PreAgentWeb by lazy {
        AgentWeb.with(this)
            .setAgentWebParent(mBinding.flWeb, FrameLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .createAgentWeb()
            .ready()
    }

    override fun FragmentWebViewBinding.initView(savedInstanceState: Bundle?) {
        agentWeb = preWeb.go(webInfo.url)
        lifecycleOwner?.lifecycle?.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                when (event) {
                    Lifecycle.Event.ON_RESUME -> agentWeb?.webLifeCycle?.onResume()
                    Lifecycle.Event.ON_PAUSE -> agentWeb?.webLifeCycle?.onPause()
                    Lifecycle.Event.ON_DESTROY -> agentWeb?.webLifeCycle?.onDestroy()
                }
            }
        })

        initToolbar(webInfo.title.toHtml()) {
            navigateUp()
        }
    }

    override fun initData() {

    }

    override fun FragmentWebViewBinding.initListener() {
        mActivity.onBackPressedDispatcher.addCallback(viewLifecycleOwner,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    navigateUp()
                }
            })
    }

    private fun navigateUp() {
        agentWeb?.also { aw ->
            if (aw.webCreator.webView.canGoBack()) {
                aw.webCreator.webView.goBack()
            } else {
                findNavController().navigateUp()
            }
        }
    }

    companion object {
        fun start(
            fragment: BaseFragment<*>,
            id: Int = -1,
            url: String,
            title: String,
            collected: Boolean = false,
            collectedType: Int = WebViewBean.TYPE_ARTICLE
        ) {
            fragment.activityNavController.navigate(
                WebViewFragmentDirections.actionToWebView(
                    WebViewBean(
                        id = id,
                        url = url,
                        title = title,
                        collected = collected,
                        collectedType = collectedType
                    )
                )
            )
        }
    }
}